using System;
using Microsoft.AspNetCore.Mvc;
using ApiProject.Api.Entity;
using System.Linq;
using ApiProject.Api.Repository;
using Microsoft.AspNetCore.Hosting;
using ApiProject.Api.Uitls;
using ApiProject.Api.Utils;
using ApiProject.Api.PramaModel;


// 轮播图管理
namespace ApiProject.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class CarouselpicturesController : ControllerBase
    {
        //广告轮播图


        private IRepository<AdImage> _adImage;

        private readonly IWebHostEnvironment _hostingEnvironment;

        public CarouselpicturesController(IWebHostEnvironment hostingEnvironment, IRepository<AdImage> adImage)
        {
            _hostingEnvironment = hostingEnvironment;
            _adImage = adImage;

        }


        //添加广告轮播图
        //Post 
        [HttpPost]
        public dynamic AddPicture()
        {
            var files = HttpContext.Request.Form.Files;
              var name = HttpContext.Request.Form["adImageLink"].ToString();
            
          

            string webRootPath = _hostingEnvironment.WebRootPath; // ...Api/wwwroot
            var showfilePath = PicHelper.getFilePath(files, "upload", webRootPath);

            Console.WriteLine(showfilePath);
             Console.WriteLine(name);

            var adImage = new AdImage
            {
                AdImagePath = showfilePath,
                AdImageLink=name,
            };

            _adImage.Insert(adImage);

            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = adImage,
                Msg = "添加图片成功"
            });
        }



         //获取所有的轮播图
        //Get /carouselpictures/cps
        [HttpGet,Route("lbt")]
        public dynamic GetPicture()
        {
            var alllbt = _adImage.Table.Where(x => x.IsDelete == false).ToList();

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = alllbt
            });
        }


        //编辑轮播图
        //Put /carouselpictures/editcar/${id}
        [HttpPut,Route("editcar/{id}")]
        public dynamic EditCarPicture(int id){
             var files = HttpContext.Request.Form.Files;
               var name = HttpContext.Request.Form["adImageLink"].ToString();

            var newpic = _adImage.GetById(id);

            string webRootPath = _hostingEnvironment.WebRootPath; // ...Api/wwwroot
            var showfilePath = PicHelper.getFilePath(files, "upload", webRootPath);

            newpic.AdImagePath = showfilePath;
            newpic.AdImageLink=name;
            newpic.UpdateTime = DateTime.Now;

            _adImage.Update(newpic);

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = newpic
            });

        }




         //是否启用禁用轮播图
        //Post /carouselpictures/isenablepic/${id}
        [HttpPost, Route("isenablepic/{id}")]
        public dynamic IsEnable(int id, NewIsEnable model)
        {
            var pictures = _adImage.GetById(id);

            var num = model.IsEnable;

            if (pictures != null)
            {
                if (num == false)
                {
                    pictures.IsEnable = false;
                    pictures.UpdateTime = DateTime.Now;
                    _adImage.Update(pictures);
                    return JsonHelper.Serialize(new
                    {
                        Code = 202,
                        Data = pictures,
                        Msg = string.Format("已禁用", id)
                    });
                }
                else
                {
                    pictures.IsEnable = true;
                    pictures.UpdateTime = DateTime.Now;
                    _adImage.Update(pictures);
                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = pictures,
                        Msg = string.Format("已启用", id)
                    });
                }

            }
            else
            {
                return JsonHelper.Serialize(new { Code = 1000, Data = "", Msg = "禁用失败" });
            }
        }


        //是否删除轮播图
        //Post /carouselpictures/deletepic/${id}
        [HttpPost, Route("deletepic/{id}")]
        public dynamic DeleteCode(int id)
        {

            var pictures = _adImage.GetById(id);

            pictures.IsDelete = true;
            pictures.UpdateTime = DateTime.Now;

            _adImage.Update(pictures);

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = pictures,
                Msg = "删除轮播图成功"
            });
        }






    }
    
}